package view;

import domain.House;
import service.HouseService;
import utils.Utility;

public class HouseView {
    private boolean loop = true;
    private char key;
    private HouseService houseservice=new HouseService(2);

    //查找
    public void findHouse() {
        System.out.println("==========房屋查找=========");
        System.out.print("请输入要查找的房屋id:");
        int id=Utility.readInt();
        if(houseservice.find(id)==null) {
            System.out.println("未查找到！");
        }else{
            System.out.println(houseservice.find(id));
            System.out.println("==========查找完成=========");
        }
    }
    //退出确认
    public void exit(){
        char choice=Utility.readConfirmSelection();
        if(choice=='Y'){
            loop=false;
        }
    }
    //删除界面，接收一个房屋编号
    public void delHouse(){
        System.out.println("==========房屋删除=========");
        System.out.println("请输入待删除房屋编号(-1退出)：");
        int delId=Utility.readInt();
        if(delId==-1){
            System.out.println("============您已取消房屋删除=========");
            return;
        }
        char choice=Utility.readConfirmSelection();
        if(choice=='Y'){
            if(houseservice.del(delId)){
                System.out.println("==========房屋已删除=========");
            }else {
                System.out.println("==========房屋编号错误=========");
            }
        }else{
            System.out.println("============您已取消房屋删除=========");
        }
    }

    public void addHouse() {
        System.out.println("==========房屋出租=========");
        System.out.println("姓名：");
        String name=Utility.readString(8);
        System.out.println("电话：");
        String address=Utility.readString(11);
        System.out.println("地址：");
        String phone=Utility.readString(12);
        System.out.println("月租：");
        int rent=Utility.readInt();
        System.out.println("状态：");
        String state=Utility.readString(3);
        House house = new House(0,name,address,phone,rent,state);
        if(houseservice.add(house)){
            System.out.println("=======添加房屋成功======");
        }else{
            System.out.println("=======添加房屋失败======");
        }
    }

    public void listHouse() {
        System.out.println("===========房屋列表==========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses=houseservice.list();
        for(House i:houses) {
            if(i==null) {
                continue;
            }
            System.out.println(i);
        }
        System.out.println("=========房屋列表显示完毕========");
    }

    public void mainMenu()
    {
        do {
            System.out.println("========房屋出租系统菜单=======");
            System.out.println("\t\t\t1.新 增 房 源");
            System.out.println("\t\t\t2.查 找 房 屋");
            System.out.println("\t\t\t3.删 除 房 屋 信 息");
            System.out.println("\t\t\t4.修 改 房 屋 信 息");
            System.out.println("\t\t\t5.房 屋 列 表");
            System.out.println("\t\t\t6.退 出");
            System.out.println("请输入你的选择(1-6):");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("你的输入有误！");
                    break;
            }
        }
        while (loop);
    }

}
